//
//  LYHomeTableViewController.swift
//  微博
//
//  Created by liuerying on 16/5/19.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit

import SVProgressHUD

private let HomeCellID = "HomeCellID"
class LYHomeTableViewController: LYBaseTableViewController {

//    下拉刷新的控件
    let refreshView = LYRefreshControl()
    //定义模型数组
//    lazy var statues: [LYStatuses] = [LYStatuses]()

    //添加视图模型属性

    lazy var statusListViewModel: LYStatusListViewModel = LYStatusListViewModel()

    
    override func viewDidLoad() {
        super.viewDidLoad()

        //如果用户未登录,设置访客视图信息
        if !userLogin{
        
            visitorLoginView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)

            return
            
        }
        
        setNavBar()
        
        //设置tableView
        prepareForTableView()
        
        loadData()
        
    }

    
   private func prepareForTableView(){
    
    
    //预估行高
    tableView.rowHeight = UITableViewAutomaticDimension
    
    //设置tableView的分割线 隐藏
    tableView.separatorStyle = .None

    tableView.estimatedRowHeight = 300
    //注册cell
    tableView.registerClass(LYStatusCell.self, forCellReuseIdentifier: HomeCellID)
    
    refreshView.addTarget(self, action: #selector(LYHomeTableViewController.loadData), forControlEvents: .ValueChanged)
    
  
    self.tableView.addSubview(refreshView)
    
    tableView.tableFooterView = pullupView
   
    //添加到导航条上
    self.navigationController?.navigationBar.addSubview(tipLabel)
    //移动到最底层
    self.navigationController?.navigationBar.insertSubview(tipLabel, atIndex: 0)
    
    }
    
    
 ///设置导航条
    func setNavBar(){
    


        
        let popItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(LYHomeTableViewController.btnClick))
        
     
        navigationItem.rightBarButtonItem = popItem
        
    }
    
    //右边按钮点击事件
 @objc private func btnClick(){
 
    let detailVC = LYDetailViewController()
 
    //隐藏tabbar
    detailVC.hidesBottomBarWhenPushed = true
 
    
    self.navigationController?.pushViewController(detailVC, animated: true)
    
        
    }
    
    //懒加载小菊花控件
    private lazy var pullupView:UIActivityIndicatorView = {
    
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        
        return indicatorView
    }()

    //懒加载 显示数字
    private lazy var tipLabel: UILabel = {
    
        let l = UILabel(title: "", font: 15, textcolor: UIColor.whiteColor())
        
        //Y要是负的
        l.frame = CGRectMake(0, -64, ScreeWidth, 40)
        
        //设置背景颜色
        l.backgroundColor = UIColor.redColor()
        
        return l
    }()
   
}

extension LYHomeTableViewController{
    
   @objc private func loadData(){
//
        statusListViewModel.loadData(pullupView.isAnimating()) { (isSucces,count) in
      
            //停止小菊花的转动
//            self.pullupView.stopAnimating()
            
            self.refreshView.endRefreshing()
            
           //判断如果不成功,给出提示
            if !isSucces{
           
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
            
                return
            }
            
            if self.pullupView.isAnimating(){
            
                self.pullupView.stopAnimating()
            }else{
            //下拉刷新提示刷新多少条数据
                self.starAnimation(count)
            }
            //如果成功,就刷新界面
            self.tableView.reloadData()
        }
        
        


 
    }
    
    private func starAnimation(count:Int){
    
        let tipText = (count == 0) ? "没有数据" : "有\(count)条数据"
        
        tipLabel.text = tipText
        
        let rect = tipLabel.frame
        
        UIView.animateWithDuration(1, animations: {
            self.tipLabel.frame = CGRectMake(0, 44, ScreeWidth, 40)
            }) { (_) in
                
                UIView.animateWithDuration(0.6, animations: {
                    self.tipLabel.frame = rect
                    }, completion: { (_) in
                        print("ok")
                })
        }
    }
}


extension LYHomeTableViewController{

    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusViewModelArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! LYStatusCell
       
        
        if indexPath.row == statusListViewModel.statusViewModelArray.count - 1 && !pullupView.isAnimating(){
        
            pullupView.startAnimating()
            loadData()
        }
        
        cell.statuesModel = statusListViewModel.statusViewModelArray[indexPath.row]
       
        
        return cell
    }

}










